<?php

namespace app\common\model;

use think\Model;
use think\Db;

/**
 * 砍价活动用户参与进度信息模型
 */
class CutdownFollow extends Model
{
    protected $append = ['yikan_text','daojishi_text'];

    public function cutdown(){
        return $this->belongsTo('Cutdown','cutdown_id','id')->field('id,description,price,min_price,time_limit,sales_sum,goods_id,status');
    }

    public function goods(){
        return $this->belongsTo('Goods','goods_id','id')->field('id,image');
    }

    public function cutdownList(){
        return $this->hasMany('CutdownList','cutdown_follow_id','id')->order('cut_price desc');
    }

    public function getYiKanTextAttr($value,$data){
        $price = Db::name('cutdown')->where(['id' => $data['cutdown_id']])->value('price');
        return ($price*100 - $data['deal_price']*100)/100;
    }

    public function getDaojishiTextAttr($value,$data){
        $time_limit = Db::name('cutdown')->where('id',$data['cutdown_id'])->value('time_limit');
        $cha = time() - $data['createtime'];
        ($time_limit - $cha > 0) ? $daojishi = $time_limit - $cha : $daojishi = 0 ;

        return $daojishi;
    }





}